/**
 * @(#)Data.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 2 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 *
 * History:
 *
 * Date          Author              Change
 * ----------    ----------------    ---------------------------------------------------------------
 * 07/15/2003    Jung Soo Kim        Initial version.
 */


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;


/**
 * A data node provides access to student and course data including reading the record information
 * and writing registration information. Note that current version of data node doesn't support
 * writing student and course records to files. A shutdown means loss of all registration
 * information.
 */
public class UserData extends UnicastRemoteObject implements RIUserData {

    private static final String UserDATA_NAME = "UserData";

    /**
     * A list of <code>Student</code> objects containing student records.
     */

    /**
     * A list of <code>Course</code> objects containing course records.
     */

    protected ArrayList vUser;    
    protected ArrayList vCurUser;
    
    /**
     * Construct a data node that provides access to student and course data. Initial data are
     * filled with the records in the given student and course record files. At the time of
     * creation, the data node does not contain any registration information.
     *
     * @param sStudentFileName the name of student record file
     * @param sCourseFileName the name of course record file
     */
    public UserData()
           throws RemoteException, FileNotFoundException, IOException {
        // Open the given student and course files.
    /*    BufferedReader objStudentFile = new BufferedReader(new FileReader(sStudentFileName));
        BufferedReader objCourseFile  = new BufferedReader(new FileReader(sCourseFileName));
*/
        // Initialize student and course lists.
        this.vUser = new ArrayList();
        this.vCurUser = new ArrayList();
/*
        // Populate student and course lists.
        while (objStudentFile.ready()) {
            this.vStudent.add(new Student(objStudentFile.readLine()));
        }
        while (objCourseFile.ready()) {
            this.vCourse.add(new Course(objCourseFile.readLine()));
        }

        // Close the student and course files.
        objStudentFile.close();
        objCourseFile.close();
  */
    }

    public void addCurUser(String sID){
    	this.vCurUser.add(sID);
    }
    
    public void addUserID(String sID, String sPasswd){
    	this.vUser.add(new User(sID,sPasswd));
    }
    /**
     * Return all student records as a list.
     *
     * @return an <code>ArrayList</code> of <code>Student</code> objects containing student records
     */
    
    public ArrayList getCurUserRecords(){
    	return this.vCurUser;
    }
    public ArrayList getAllUserRecords() {
        // Return the student list as it is.
        return this.vUser;
    }

    /**
     * Creates a data node and starts it. Three parameters are expected, the first one as student
     * file name, the second one as course file name, and the third one as data node name.
     *
     * @param args array of input parameters
     */
    public static void main(String args[]) {
        // Check the number of parameters.
        if (args.length != 0) {
            System.err.println("Incorrect number of parameters");
            System.err.println("Usage: java UserData");
            System.exit(1);
        }

        try {
            // Create a data node and make it available through RMI.
            UserData objUserData = new UserData();
            Naming.bind(UserDATA_NAME, objUserData);
            System.out.println("User Data node is ready to serve.");

            // Wait for user interrupt.
            System.out.println("Press Enter to terminate.");
            System.in.read();

            // Clean up and finish.
            Naming.unbind(UserDATA_NAME);
            System.out.println("User Data node is leaving, bye. Press ctrl-c if the delay is too long.");
        }
        catch (java.rmi.ConnectException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if rmiregistry is running.");
            System.exit(1);
        }
        catch (Exception e) {
            // Dump the exception information for debugging.
            System.err.println("Unexpected exception at " + UserDATA_NAME);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
